package com.qf.springcloud.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.springcloud.client.AirSearchClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @author zjw
 * @description
 * @date 2021/11/9 17:46
 */
@RestController
@RefreshScope
public class AirCoreController {


    @Autowired
    private AirSearchClient airSearchClient;

    @Value("${aircore.version}")
    private String version;

    @GetMapping("/version")
    public String version(){
        return version;
    }


    @GetMapping("/air/core")
    @HystrixCommand(fallbackMethod = "coreFallback",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
//            @HystrixProperty(name = "execution.timeout.enabled", value = "false"),
//            @HystrixProperty(name = "execution.isolation.thread.interruptOnTimeout", value = "true"),
//            @HystrixProperty(name = "execution.isolation.thread.interruptOnCancel", value = "false"),
//            @HystrixProperty(name = "execution.isolation.semaphore.maxConcurrentRequests", value = "10"),
    })
    public String core() throws InterruptedException {
        System.out.println("当前线程名为：" + Thread.currentThread().getName());
        String result = airSearchClient.search();
        return "ok!";
    }

    public String coreFallback(){
        return "服务器挤爆了，请稍后再试！";
    }


    @GetMapping("/air/core/delete/{id}")
    @HystrixCommand(fallbackMethod = "deleteFallback",commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "5"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "500000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "10"),
            @HystrixProperty(name = "circuitBreaker.forceOpen",value = "false"),
            @HystrixProperty(name = "circuitBreaker.forceClosed",value = "false"),

    })
    public String delete(@PathVariable String id) {
        if ("1".equalsIgnoreCase(id)){
            throw new RuntimeException("爆炸！");
        }
        String result = airSearchClient.delete(id);
        return result;
    }

    public String deleteFallback(@PathVariable String id){
        return "awsl" + id;
    }



    @PostMapping("/air/core/update")
    public String update(String id, String name, Integer age, @DateTimeFormat(pattern = "yyyy-MM-dd") Date birthday) {
        airSearchClient.update(id, name, age, birthday);
        return "ok!";
    }

    @PostMapping("/air/core/add")
    public Map add(@RequestBody Map map) {
        Map result = airSearchClient.add(map);
        return result;
    }


}
